package org.mybatis.generator.plugins.js;

import org.apache.log4j.Logger;

import java.util.ArrayList;
import java.util.List;

import org.mybatis.generator.api.GeneratedJavaFile;
import org.mybatis.generator.api.GeneratedJavaScriptFile;
import org.mybatis.generator.api.IntrospectedTable;
import org.mybatis.generator.api.PluginAdapter;
import org.mybatis.generator.api.dom.java.Interface;
import org.mybatis.generator.api.dom.java.TopLevelClass;
import org.mybatis.generator.api.dom.xml.Document;
import org.mybatis.generator.api.dom.xml.XmlElement;
import org.mybatis.generator.plugins.js.elements.ExtJsMethodGenerator;

/**
 * 生成ExtJs界面操作的所有js文件
 * 
 */
public class MybatisExtJsPlugin extends PluginAdapter {
	/**
	 * Logger for this class
	 */
	private static final Logger logger = Logger
			.getLogger(MybatisExtJsPlugin.class);

	private static int SORT_ORDER = 10036;

	private String targetPackage;
	private String targetModel;
	private String targetProject;

	@Override
	public boolean validate(List<String> warnings) {
		targetPackage = properties.getProperty("targetPackage");
		targetModel = properties.getProperty("targetModel");
		targetProject = properties.getProperty("targetProject");
		return true;
	}

	@Override
	public List<GeneratedJavaFile> contextGenerateAdditionalJavaFiles(
			IntrospectedTable introspectedTable) {

		// 增加结束处
		return null;
	}

	@Override
	public boolean clientGenerated(Interface interfaze,
			TopLevelClass topLevelClass, IntrospectedTable introspectedTable) {
		// 接口方法
		return true;
	}

	@Override
	public boolean sqlMapDocumentGenerated(Document document,
			IntrospectedTable introspectedTable) {
		return true;
	}

	@Override
	public boolean sqlMapSelectByExampleWithoutBLOBsElementGenerated(
			XmlElement element, IntrospectedTable introspectedTable) {
		return true;
	}

	@Override
	public boolean sqlMapSelectByExampleWithBLOBsElementGenerated(
			XmlElement element, IntrospectedTable introspectedTable) {
		return true;
	}

	@Override
	public List<GeneratedJavaScriptFile> contextGenerateAdditionalJavaScriptFiles() {
		// TODO Auto-generated method stub
		List<GeneratedJavaScriptFile> files = new ArrayList<GeneratedJavaScriptFile>(
				1);
		return files;
	}

	@Override
	public List<GeneratedJavaScriptFile> contextGenerateAdditionalJavaScriptFiles(
			IntrospectedTable introspectedTable) {
		String tableName = introspectedTable.getTableConfiguration()
				.getTableName();
		String tableLab = introspectedTable.getRemarks();
		while (tableName.lastIndexOf("_") != -1) {
			int index = tableName.lastIndexOf("_");
			tableName = tableName.replaceFirst(
					"_" + tableName.substring(index + 1, index + 2), tableName
							.substring(index + 1, index + 2).toUpperCase());
		}
		tableName=toLowerCase(tableName);
		List<GeneratedJavaScriptFile> files = new ArrayList<GeneratedJavaScriptFile>();

		ExtJsMethodGenerator emg = new ExtJsMethodGenerator(introspectedTable,
				targetModel);

		GeneratedJavaScriptFile controllerFile = new GeneratedJavaScriptFile(
				emg.getControllerMethod(), properties.getProperty("fileName",
						tableName + "Controller.js"), targetPackage
						+ ".controller", targetProject, true);

		GeneratedJavaScriptFile modelFile = new GeneratedJavaScriptFile(
				emg.getModelMethod(), properties.getProperty("fileName",
						tableName + "Model.js"), targetPackage + ".model",
				targetProject, true);

		GeneratedJavaScriptFile storeFile = new GeneratedJavaScriptFile(
				emg.getStoreMethod(), properties.getProperty("fileName",
						tableName + "Store.js"), targetPackage + ".store",
				targetProject, true);

		GeneratedJavaScriptFile storeAllFile = new GeneratedJavaScriptFile(
				emg.getAllStoreMethod(), properties.getProperty("fileName",
						tableName + "AllStore.js"), targetPackage + ".store",
				targetProject, true);

		GeneratedJavaScriptFile viewFile = new GeneratedJavaScriptFile(
				emg.getViewMethod(), properties.getProperty("fileName",
						tableName + "View.js"), targetPackage + ".view",
				targetProject, true);

		GeneratedJavaScriptFile winFile = new GeneratedJavaScriptFile(
				emg.getWinMethod(), properties.getProperty("fileName",
						tableName + "Win.js"), targetPackage + ".view",
				targetProject, true);
		
		GeneratedJavaScriptFile winShowFile = new GeneratedJavaScriptFile(
				emg.getShowWinMethod(), properties.getProperty("fileName",
						tableName + "ShowWin.js"), targetPackage + ".view",
				targetProject, true);
		
		files.add(controllerFile);
		files.add(modelFile);
		files.add(storeFile);
		files.add(storeAllFile);
		files.add(viewFile);
		files.add(winFile);
		files.add(winShowFile);
		int parentId = 1003;
		
		System.out.println("INSERT INTO `Permission` VALUES ("+parentId+""+SORT_ORDER+",'" + tableLab + "','"
				+ parentId + "', '"+ targetModel + "_" + tableName + "','" +2 + "','1','','','"
				+ SORT_ORDER + "','',  'yy." + targetModel
				+ ".controller." + tableName + "Controller', 'yy."
				+ targetModel + ".view." + tableName + "View', '" + tableName
				+ "View','1','"+tableName+"', '2018-7-04 9:11:30', '2018-7-04 9:11:30');");
		
		for (int i = 1; i < 5; i++) {
			String lab = "";
			String code = "";
			switch (i) {
			case 1:
				lab = "添加";
				code = "add";
				break;
			case 2:
				lab = "修改";
				code = "edit";
				break;
			case 3:
				lab = "删除";
				code = "delete";
				break;
			case 4:
				lab = "查看";
				code = "show";
				break;
			}
			
			//System.out.println("INSERT INTO `Permission` VALUES ("+parentId+""+SORT_ORDER+""+i+",'" + lab + "','"
			//		+ parentId+""+SORT_ORDER + "', '" + 1 + "','','','"
			//		+ i + "',  '" + targetModel + "_"
			//		+ tableName + "_" + code + "', '', '','1','"+tableName+"-"+lab+"', '2018-7-04 9:11:30', '2018-7-04 9:11:30');");
			
			System.out.println("INSERT INTO `Permission` VALUES ("+parentId+""+SORT_ORDER+""+i+",'" + lab + "','"
					+ parentId + "" + SORT_ORDER + "', '"+ targetModel + "_" + tableName +"_" + code +"','" + 1 + "','1','','','"
					+ SORT_ORDER + "','',  'yy." + targetModel
					+ ".controller." + tableName + "Controller', 'yy."
					+ targetModel + ".view." + tableName + "View', '" + tableName
					+ "View','1','"+tableName+ "_" + code +"', '2018-7-04 9:11:30', '2018-7-04 9:11:30');");
			
			//System.out.println("INSERT INTO `Permission` VALUES ("+parentId+""+SORT_ORDER+""+i+",'" + parentId
			//		+ "" + SORT_ORDER + "" + i + "', '" + lab + "', '"
			//		+ parentId + "" + SORT_ORDER + "','" + targetModel + "_"
			//		+ tableName + "_" + code + "', '2', null, null, '" + i
			//		+ "', '1', '', '', '','1','', '2015-7-10 9:11:30', '2018-7-04 9:11:30');");
		}

		// System.out.println("alter table "+tableName_temp+" MODIFY id INT UNSIGNED AUTO_INCREMENT;");
		// INSERT INTO `permission` VALUES ('1', '系统管理', null, '1', null, null,
		// '1', '1', '', '', '', '1','', '2014-10-09 00:11:30');
		SORT_ORDER++;
		return files;
	}
	
	
	/**
	 * BaseUsers to baseUsers
	 * 
	 * @param tableName
	 * @return
	 */
	protected String toLowerCase(String tableName) {
		StringBuilder sb = new StringBuilder(tableName);
		sb.setCharAt(0, Character.toLowerCase(sb.charAt(0)));
		return sb.toString();
	}

	/**
	 * BaseUsers to baseUsers
	 * 
	 * @param tableName
	 * @return
	 */
	protected String toUpperCase(String tableName) {
		StringBuilder sb = new StringBuilder(tableName);
		sb.setCharAt(0, Character.toUpperCase(sb.charAt(0)));
		return sb.toString();
	}

}
